// Created By Nicky
var SH,SW,oldX=0,oldY=0,frameTimer=null;
var tcount=0,ccount=0 , loadtimer=null , loadcoy=0;

for(i=0;i<6;i++){
	$('#load_pic').append('<img src="img/loadimg/loadbg'+i+'.png" class="ab w h ld_img'+i+'" />');
}

loadtimer = setInterval(function(){ 
	if(loadcoy<6){
		$('#load_pic img').hide(); 
		$('#load_pic img.ld_img'+loadcoy).show(); 
	}
	loadcoy = loadcoy+1;
	if(loadcoy>20){
		loadcoy =0;
	}
},100);

var FRAME_IMG_DATA = [
	{src:"img/zf/zf (#).png",count:189} ,
	{src:"img/snow/snow#.png",count:4} 
];


/* 动画场景设置 */
var FrameConfig = {
		timer:null,
		pid:'framebox',
		src:"img/zf/zf (#).png",
		timespan:120,
		frameCount:189,
		className:'ab w hd',
		step:1,
 
		pause:false,
		stop:false,
		
		curFrame:1,
		step_conf:{}
}; 
//汽车进场动画
FrameConfig.step_conf[1] = {start:1,end:28,loop:false,
	Event:{ 
		End:function(){ 
			playFrame(2); 
			$('#carbg').addClass('carbgBg'); 
		}
}};
//背景放大
FrameConfig.step_conf[2] = {start:28,end:38,loop:false,
	Event:{ 
		End:function(){ 
			playFrame(3); 
			$('#carbg').addClass('carbgBgEnd').removeClass('carbgBg'); 
		}
}};

FrameConfig.step_conf[3] = {start:39,end:89,loop:false,
	Event:{ 
		End:function(){ 
			$('#recordbtn,#recordbtnsc,,#recordbtnsc2,#recordbtnbg,#recordviewbtn').width(SW*0.4).height(SW*0.4).css({'top':SH*0.37,'left':SW*0.3});
			$('#recordbtn,#recordbtnbg').fadeIn(); 
			$('.music_btn').removeClass('music_btn_on'); 
			document.getElementById('index_music').pause();
			$('#touchtxt img').fadeOut();
			$('#touchtxt,#touchtxt .txt_start').fadeIn();
			$('#recordbtn,#recordbtnsc,#recordbtnbg').bind('touchstart',function(ev){ ev.preventDefault(); });
			playFrame(6);
		}
}};

FrameConfig.step_conf[4] = {start:116,end:129,loop:false,
	Event:{ 
		Start:function(){},
		End:function(){
			$('#bg').fadeOut();
			$('#cardbtn').fadeIn();
			playFrame(5);
		}
}};

FrameConfig.step_conf[5] = {start:130,end:142,loop:false, Event:{  }};
//聆听动画
FrameConfig.step_conf[6] = {start:90,end:115,loop:true, Event:{ }};

function InitFrame(){
	$('#'+FrameConfig.pid).empty();
	for(i=1;i<=FrameConfig.frameCount;i++){ 
		html = '<img class="'+FrameConfig.pid+'_'+i +' '+ FrameConfig.className +'" src="'+FrameConfig.src.replace(/#/,i)+'">';
		$('#'+FrameConfig.pid).append(html);
	}
}

function playFrame(step){
	//场景信息
	FrameConfig.step = step;
	FrameConfig.pause=false;
	FrameConfig.stop=false;
	
	//场景参数信息
	var Step_Info = FrameConfig.step_conf[FrameConfig.step];
	var START_FRAME = Step_Info['start'];
	var END_FRAME = Step_Info['end'];
	var LOOP = Step_Info['loop'];
	
	var loopCount = 0;
	//播放场景动画
	FrameConfig.curFrame = START_FRAME;
	
	clearInterval(FrameConfig.timer);
	try{
		Step_Info.Event.Start(); 
	}catch(e){}
	FrameConfig.timer = setInterval(function(){
		if(!FrameConfig.pause&&!FrameConfig.stop){
			$('#'+FrameConfig.pid+" img").hide();
			$('#'+FrameConfig.pid+" ."+FrameConfig.pid+"_"+FrameConfig.curFrame).show();
			FrameConfig.curFrame++; 
			if(FrameConfig.curFrame>=END_FRAME){
				if(LOOP){
					FrameConfig.curFrame = START_FRAME;
					try{
						Step_Info.Event.Loop(); 
					}catch(e){}
					if(loopCount<1){
						try{
							Step_Info.Event.FirstLoop(); 
						}catch(e){}
					}
					loopCount++;
					if(Step_Info.loopCount==loopCount){
						FrameConfig.pause = true;
						try{
							Step_Info.Event.End(); 
						}catch(e){}
					}
				}else{
					FrameConfig.pause = true;
					try{
						Step_Info.Event.End(); 
					}catch(e){}
				}
			}
		}
	},FrameConfig.timespan);
}

var IMG_ARRAY = [];

$(function(){
	$('body').unbind('touchmove').bind('touchmove',function(ev){ ev.preventDefault(); });
	//预加载页面图片
	$.each($('img'),function(i,d){
		IMG_ARRAY[tcount++] = $(d).attr('src');
	});
	//预加载Frame图片
	$.each(FRAME_IMG_DATA,function(k,d){
		for(i=1;i<d.count+1;i++){
			IMG_ARRAY[tcount++] = d.src.replace(/#/,i);
		}
	});
	var images = [];
	$.each(IMG_ARRAY,function(i,d){
		 images[i] = new Image();
         images[i].onload = function(){
            ccount++;
			$('#load_txt').text(parseInt(100*ccount/tcount)+'%');  
			if(ccount+4>tcount) setTimeout(function(){ $('#load').fadeOut();  LoadCallBack(); clearInterval(loadtimer); },1000); 
       }
       images[i].src = d; 
	}); 
});

function LoadCallBack(){
	window.onorientationchange = orientationChange; 
	$('#sc').html('<table><tr><td><img id="sc_img" src="img/sc.png" /></td></tr></table>');
	orientationChange();
	$('#indexpg,#logo,#music_btn').fadeIn();
	InitFrame()
	$('.music_btn').bind('dblclick',function(ev){ ev.preventDeafult(); }).bind('touchstart',function(){ 
		if($(this).hasClass('music_btn_on')){
			$(this).removeClass('music_btn_on');
			document.getElementById('index_music').pause();
		}else{
			$(this).addClass('music_btn_on');
			document.getElementById('index_music').play();
		}
	});
	
	$('#indexpg').bind('touchstart',function(){
		createSnow('', 10);
		$('#indexbg').fadeOut(); 
		$('#framebox').fadeIn();
		$('.music_btn').addClass('music_btn_on'); 
		playFrame(1);
		_smq.push(['custom','2015CNY','Start']);
	});
	
	$('#reset_btn').bind('touchstart',function(){ 
		$('#recordbtn').fadeIn();
		$('#recordviewbtn , #bottom_btn').fadeOut();
		$('#touchtxt,#touchtxt img').hide();
		$('#touchtxt,#touchtxt .txt_start').fadeIn();
	});
	
	$('#cardbg,#cardbg img').bind('touchmove',function(ev){
		ev.preventDefault();
	}).bind('dblclick',function(ev){ ev.preventDeafult(); });
	
	$('#cardshareBtn').bind('touchstart',function(ev){
		ev.preventDefault(); 
		$('#music_btn,#logo').fadeOut();
		$('#sharediv').fadeIn();
		_smq.push(['custom','2015CNY','Share']);
	});

	$('#sharediv').bind('touchstart',function(ev){
		ev.preventDefault();
		$('#music_btn,#logo').fadeIn();
		$('#sharediv').fadeOut();
	});

	$('#cardshareBtn2').click(function(){
		$('#cardbtn').hide();
		playFrame(6);
		$('#touchtxt .txt_end').hide();
		$('#recordbtn,#recordbtnbg,#touchtxt, #touchtxt .txt_start').fadeIn();
		$('#recordbtn,#recordbtnbg,.txt_start,#cardshareBtn,.cardshareTxt').fadeIn();
		$('#cardshareBtn2').hide();
		voice = {
			localId: '',
			serverId: ''
		};
	});
	
	$('#framebox').bind('touchmove',function(ev){
		ev.preventDefault();
	});

	$('.indexpoint').click(function(){
		createSnow('', 10);
		$('#indexbg').fadeOut(); 
		$('#framebox').fadeIn();
		$('.music_btn').addClass('music_btn_on'); 
		playFrame(1);
		_smq.push(['custom','2015CNY','Start']);
	});
}



//初始化界面 获取宽和高
function InitPage(){
	SW = $(window).width();
	SH = $(window).height();
}

//手机屏幕旋转
function orientationChange(){ 
	InitPage();	 
	if(window.orientation==180||window.orientation==0){
		var TH = $(window).width()/640*1008;
		$('#recordbtn,#recordbtnsc,,#recordbtnsc2,#recordbtnbg,#recordviewbtn').width(SW*0.4).height(SW*0.4).css({'top':TH*0.37,'left':SW*0.3});
		$('#sc').fadeOut();
	}
	if(window.orientation==90||window.orientation==-90){ 
		$('#sc').fadeIn();
	}
}
 